package com.example.demospring.controller.assistant;

import com.example.demospring.dto.assistant.ConsultingAppointmentDTO;
import com.example.demospring.entity.ConsultingAppointment;
import com.example.demospring.service.assistant.ConsultingAppointmentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demospring.dto.assistant.ConsultingAppointmentWithAuditsDTO;
import com.example.demospring.service.assistant.ConsultingService;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/assistant/appointments")
public class ConsultingAppointmentController {

    @Autowired
    private ConsultingAppointmentService service;

    @Autowired
    private ConsultingService consultingService;



    // 查询所有预约，支持按状态筛选（如无status参数则查全部）
    @GetMapping
    public List<ConsultingAppointmentDTO> getAppointments(
            @RequestParam(value = "status", required = false) String status
    ) {
        List<ConsultingAppointment> list;
        if (status == null || status.isEmpty()) {
            list = service.findAll();
        } else {
            list = service.findByStatus(ConsultingAppointment.Status.valueOf(status));
        }
        return list.stream().map(this::toDTO).collect(Collectors.toList());
    }

    // 获取单条预约
    @GetMapping("/{id}")
    public ConsultingAppointmentDTO get(@PathVariable Integer id) {
        ConsultingAppointment appointment = service.findById(id)
                .orElseThrow(() -> new RuntimeException("not found"));
        return toDTO(appointment);
    }

    // 创建/修改预约
    @PostMapping
    public ConsultingAppointmentDTO createOrUpdate(@RequestBody ConsultingAppointmentDTO dto) {
        ConsultingAppointment a = toEntity(dto);
        ConsultingAppointment saved = service.save(a);
        return toDTO(saved);
    }


    /**
     * 删除预约及对应从表
     * DELETE /api/consulting-appointments/{id}
     */

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        consultingService.deleteAppointmentAndAllRelated(id);
    }

    // DTO <-> entity 转换方法
    private ConsultingAppointmentDTO toDTO(ConsultingAppointment a) {
        ConsultingAppointmentDTO dto = new ConsultingAppointmentDTO();
        dto.setId(a.getId());
        dto.setStudentId(a.getStudentId());
        dto.setShiftId(a.getShiftId());
        dto.setCounselorId(a.getCounselorId());
        dto.setStatus(a.getStatus() != null ? a.getStatus().name() : null);
        dto.setApplyDate(a.getApplyDate());
        dto.setReason(a.getReason());
        dto.setInitialVisitConclusion(a.getInitialVisitConclusion());
        dto.setArrangedDate(a.getArrangedDate());
        dto.setTimeSlot(a.getTimeSlot());
        dto.setLocation(a.getLocation());
        dto.setRemark(a.getRemark());
        dto.setInterviewerName(a.getInterviewerName());
        return dto;
    }

    private ConsultingAppointment toEntity(ConsultingAppointmentDTO dto) {
        ConsultingAppointment a = new ConsultingAppointment();
        a.setId(dto.getId());
        a.setStudentId(dto.getStudentId());
        a.setShiftId(dto.getShiftId());
        a.setCounselorId(dto.getCounselorId());
        a.setStatus(dto.getStatus() != null ? ConsultingAppointment.Status.valueOf(dto.getStatus()) : ConsultingAppointment.Status.待审核);
        a.setApplyDate(dto.getApplyDate());
        a.setReason(dto.getReason());
        a.setInitialVisitConclusion(dto.getInitialVisitConclusion());
        a.setArrangedDate(dto.getArrangedDate());
        a.setTimeSlot(dto.getTimeSlot());
        a.setLocation(dto.getLocation());
        a.setRemark(dto.getRemark());
        a.setInterviewerName(dto.getInterviewerName());
        return a;
    }
}